# -*- coding: utf-8 -*-

# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="Eagle"
__date__ ="$12.4.2011 22:41:33$"

import helpers.dbmanager
import DB_layer.DB_local
import gdata.service
import gdata.spreadsheet.text_db

class MainModel():
    def __init__(self):
        pass

    def getSyncRequired(self):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        if self.db is None:
            return False
        return self.db.modified

    def update(self):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        return self.db.update()

    def commit(self):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        return self.db.commit()

    def validateUser(self, username, password):
        if (len(username) is 0) or (len(password) is 0):
            return False
        try:
            db = DB_layer.DB_local.database_local(username, password)
        except gdata.service.BadAuthentication:
            return False
        except gdata.spreadsheet.text_db.CaptchaRequired as (e):
            return u"Chyba při spojení s Google Docs:\n " + e.message
        except:
            return u"Chyba při spojení s Google Docs"

        dbMgr = helpers.dbmanager.DBManager.getInstance()
        dbMgr.set(db)

        return True

    def setUserName(self, username):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        self.db.username = username